package com.car.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.car.order.entity.form.serveOrderReservation.ServeOrderReservationFindListForm;
import com.car.order.entity.form.serveOrderReservation.ServeOrderReservationFindPageForm;
import com.car.order.entity.form.serveOrderReservation.ServeOrderReservationInsertForm;
import com.car.order.entity.form.serveOrderReservation.ServeOrderReservationUpdateForm;
import com.car.commons.core.model.WoniuException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.order.mapper.ServeOrderReservationMapper;
import com.car.order.entity.ServeOrderReservation;
import com.car.order.service.ServeOrderReservationService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杜岷芫
 * @since 2024-08-29 10:35:17
 */
@Service
@Slf4j
public class ServeOrderReservationServiceImpl extends ServiceImpl<ServeOrderReservationMapper, ServeOrderReservation> implements ServeOrderReservationService {

    @Resource
    private ServeOrderReservationMapper serveOrderReservationMapper;

    @Override
    public Page<ServeOrderReservation> findPage(ServeOrderReservationFindPageForm form) {
        Page<ServeOrderReservation> page = new Page<>(form.getPageNum()==null?1:form.getPageNum(),form.getPageSize()==null?6:form.getPageSize());
        QueryWrapper<ServeOrderReservation> query = new QueryWrapper<>();
        query.select("distinct *");
        query.like("reservation_user", form.getName());
        query.like("reservation_phone", form.getName());
        query.like("reservation_time", form.getDate());
        return serveOrderReservationMapper.selectPage(page, query);
    }

    @Override
    public List<ServeOrderReservation> findList(ServeOrderReservationFindListForm form){
        QueryWrapper<ServeOrderReservation> query = new QueryWrapper<>();
        return serveOrderReservationMapper.selectList(query);
    }

    @Override
    public ServeOrderReservation findById(Integer id) {
        return serveOrderReservationMapper.selectById(id);
    }

    @Override
    public ServeOrderReservation insert(ServeOrderReservationInsertForm form) {
        ServeOrderReservation serveOrderReservation = BeanUtil.toBean(form, ServeOrderReservation.class);
        boolean save = save(serveOrderReservation);
        if (!save) {
            throw new WoniuException(500, "新增失败");
        }
        return serveOrderReservation;
    }

    @Override
    public ServeOrderReservation update(ServeOrderReservationUpdateForm form) {
        ServeOrderReservation serveOrderReservation = BeanUtil.toBean(form, ServeOrderReservation.class);
        boolean b = updateById(serveOrderReservation);
        if (!b) {
            throw new WoniuException(500, "修改失败");
        }
        return serveOrderReservation;
    }

    @Override
    public void delete(Integer id) {
        int i=serveOrderReservationMapper.deleteById(id);
        if (i == 0) {
            throw new WoniuException(500, "删除失败");
        }
    }

}